<template>
  <input v-show="false" ref="input" type="file" @change="handleInputChange" />
</template>
<script>
import { postUploadFile } from '@/api/business.js';
import { getDicList } from '@/api/public.js';
export default {
  props: {
    accept: {
      type: Array,
      default: () => ['application/vnd.ms-excel', '.xls'],
    },
    visible: {
      type: [Boolean],
      default: false,
    },
    planId: {
      type: Number,
      default: 0,
    },
    list: {
      type: Array,
      default: () => [],
    },
    fileList: {
      type: Array,
      default: () => [],
    },
    stage: {
      type: [String, Number],
      default: undefined,
    },
    type: {
      type: String,
      default: '',
    },
    id: {
      type: [String, Number],
      default: undefined,
    },
    isload: {
      type: [Boolean],
      default: false,
    },
  },
  data() {
    return {
      limitFileSize: null,
      fileType: [
        'xls',
        'xlsx',
        'doc',
        'docx',
        'ppt',
        'pptx',
        'gif',
        'png',
        'pneg',
        'jpg',
        'jpeg',
        'pdf',
        'rar',
        'zip',
        'XLS',
        'XLSX',
        'DOC',
        'DOCX',
        'PPT',
        'PPTX',
        'GIF',
        'PNG',
        'PNEG',
        'JPG',
        'JPEG',
        'PDF',
        'RAR',
        'ZIP',
      ],
    };
  },
  computed: {},
  created() {
    this.getDicList();
  },
  methods: {
    getFileType(obj) {
      const index = obj.lastIndexOf('.');
      obj = obj.substring(index + 1, obj.length);
      return obj;
    },
    async getDicList() {
      const result = await getDicList('file_size');
      this.limitFileSize = result?.data?.data[0]?.value * 1 || null;
    },
    uploader() {
      this.$refs.input?.click && this.$refs.input?.click();
    },
    async handleInputChange(e) {
      const file = e.target.files[0];
      console.log('file: ', file);
      const size = e.target.files[0].size;
      const fileType = this.getFileType(file.name);
      if (this.fileType.indexOf(fileType) === -1) {
        this.$message.error('请上传规定的格式文件');
        return;
      }
      if (size > this.limitFileSize) {
        this.$message.error('请上传小于10M的文件');
        return;
      }
      const name = e.target.files[0].name;
      this.$emit('update:isload', true);
      const id = new Date().valueOf();
      const result = await postUploadFile(
        this.type,
        id,
        file,
        this.stage
      ).finally(() => {
        e.target.value = null;
        this.$emit('update:isload', false);
      });
      const data = result?.data?.data || {};
      this.list.push({
        atthName: name,
        atthAccessPath: data.atthAccessPath,
        id: data.id,
      });
      this.$emit('update:list', this.list);
      this.fileList.push(data);
      this.$emit('update:fileList', this.fileList);
    },
    handleCancel() {
      this.$emit('update:visible', false);
    },
    handleOk() {},
  },
};
</script>

<style lang="less" scoped>
.co {
  color: red;
}
</style>
